import { defineConfig, loadEnv } from 'vite'
import react from '@vitejs/plugin-react'
import { fileURLToPath } from 'url'

// https://vite.dev/config/
export default defineConfig(({ mode }) => {
  const env = loadEnv(mode, process.cwd(), '');

  return {
    server: {
      port: Number(env.VITE_APP_PORT),
    },
    plugins: [react()],
    base: '/',
    resolve: {
      alias: {
      '@': fileURLToPath(new URL('./src', import.meta.url))
    } 
  },
  build: {
    outDir: 'dist',
    assetsDir: 'assets',
    emptyOutDir: true,
    rollupOptions: {
      output: {
        manualChunks: {
          // React 相关
          'vendor-react': ['react', 'react-dom', 'react-router-dom'],
          // PDF 相关（大型库）
          'vendor-pdf': ['pdfjs-dist', 'pdf-lib'],
          // 文档处理（大型库）
          'vendor-office': ['xlsx-js-style', 'docx', 'papaparse'],
          // UI 和工具库
          'vendor-utils': ['lucide-react', 'react-icons', 'zustand', 'axios'],
          // OpenAI
          'vendor-ai': ['openai']
        }
      }
    }
  },
    define: {
      'import.meta.env.VITE_API_BASE_URL': JSON.stringify(env.VITE_API_BASE_URL)
    }
  }
})
